//
//  NSString+Encrypt.m
//  category
//
//  Created by MAC on 2018/5/12.
//  Copyright © 2018年 MAC. All rights reserved.
//

#import "NSString+Encrypt.h"
#import "NSData+Encrypt.h"
#import "NSData+Base64.h"

@implementation NSString (Encrypt)

-(NSString*)ex_encryptedWithAESUsingKey:(NSString*)key andIV:(NSData*)iv {
    NSData *encrypted = [[self dataUsingEncoding:NSUTF8StringEncoding] ex_encryptedWithAESUsingKey:key andIV:iv];
    NSString *encryptedString = [encrypted ex_base64EncodedString];
    
    return encryptedString;
}

- (NSString*)ex_decryptedWithAESUsingKey:(NSString*)key andIV:(NSData*)iv {
    NSData *decrypted = [[NSData ex_dataWithBase64EncodedString:self] ex_decryptedWithAESUsingKey:key andIV:iv];
    NSString *decryptedString = [[NSString alloc] initWithData:decrypted encoding:NSUTF8StringEncoding];
    
    return decryptedString;
}

- (NSString*)ex_encryptedWithDESUsingKey:(NSString*)key andIV:(NSData*)iv {
    NSData *encrypted = [[self dataUsingEncoding:NSUTF8StringEncoding] ex_encryptedWithDESUsingKey:key andIV:iv];
    NSString *encryptedString = [encrypted ex_base64EncodedString];
    
    return encryptedString;
}

- (NSString*)ex_decryptedWithDESUsingKey:(NSString*)key andIV:(NSData*)iv {
    NSData *decrypted = [[NSData ex_dataWithBase64EncodedString:self] ex_decryptedWithDESUsingKey:key andIV:iv];
    NSString *decryptedString = [[NSString alloc] initWithData:decrypted encoding:NSUTF8StringEncoding];
    
    return decryptedString;
}

- (NSString*)ex_encryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv {
    NSData *encrypted = [[self dataUsingEncoding:NSUTF8StringEncoding] ex_encryptedWith3DESUsingKey:key andIV:iv];
    NSString *encryptedString = [encrypted ex_base64EncodedString];
    
    return encryptedString;
}

- (NSString*)ex_decryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv {
    NSData *decrypted = [[NSData ex_dataWithBase64EncodedString:self] ex_decryptedWith3DESUsingKey:key andIV:iv];
    NSString *decryptedString = [[NSString alloc] initWithData:decrypted encoding:NSUTF8StringEncoding];
    
    return decryptedString;
}
@end
